package com.duoec.dw.lineage.service.analyser.commend;

import com.duoec.dw.lineage.dto.SqoopCommendArgument;
import com.duoec.dw.lineage.exceptions.LineageAnalyseException;
import org.springframework.stereotype.Service;

import java.util.regex.Pattern;

/**
 * @author xuwenzhen
 */
@Service
public class ConnectCommendLineAnalyser extends BaseCommendLineAnalyser {
    private static final Pattern DATABASE_NAME_PATTERN = Pattern.compile("^.+\\:\\d+/(.+)[$|\\?.*$]");

    /**
     * 用于检查的起始字段串，如果命中后就会执行
     *
     * @return 起始字段串
     */
    @Override
    public String getStart() {
        return "connect ";
    }

    /**
     * 分析
     * connect "jdbc:mysql://10.50.20.44:3306/duoec_ap_customer?zeroDateTimeBehavior=round&tinyInt1isBit=false"
     *
     * @param importDto 血缘关系数据
     * @param line      需要处理的行
     */
    @Override
    public void analyse(SqoopCommendArgument importDto, String line) {
        int index = line.indexOf("?");
        String simpleUrl = line;
        if (index > -1) {
            simpleUrl = line.substring(0, index);
        }
        index = simpleUrl.lastIndexOf("/");
        if (index > -1) {
            String databaseName = simpleUrl.substring(index + 1);
            importDto.setMySqlDatabase(databaseName);
        } else {
            throw new LineageAnalyseException("无法解析库名：" + line);
        }
    }
}
